package dao;// DepartmentDAO.java
import pojo.Department;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class DepartmentDAO {

    // 添加部门信息
    public boolean addDepartment(Department dept) {
        String sql = "INSERT INTO departments(department_name, description) VALUES (?, ?)";
        try (Connection conn = DBConnection.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setString(1, dept.getDepartmentName());
            pstmt.setString(2, dept.getDescription());
            int affected = pstmt.executeUpdate();
            return affected > 0;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }

    // 修改部门信息（根据部门名称）
    public boolean updateDepartment(Department dept) {
        String sql = "UPDATE departments SET description = ? WHERE department_name = ?";
        try (Connection conn = DBConnection.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setString(1, dept.getDescription());
            pstmt.setString(2, dept.getDepartmentName());
            int affected = pstmt.executeUpdate();
            return affected > 0;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }

    // 删除部门信息，删除前检查该部门下是否有员工
    public boolean deleteDepartment(String departmentName) {
        String checkSql = "SELECT COUNT(*) AS count FROM employees WHERE department = ?";
        try (Connection conn = DBConnection.getConnection();
             PreparedStatement checkStmt = conn.prepareStatement(checkSql)) {
            checkStmt.setString(1, departmentName);
            ResultSet rs = checkStmt.executeQuery();
            if (rs.next()) {
                int count = rs.getInt("count");
                if (count > 0) {
                    System.out.println("该部门下有员工，不能删除！请先调整员工所属部门。");
                    return false;
                }
            }
            String deleteSql = "DELETE FROM departments WHERE department_name = ?";
            try (PreparedStatement deleteStmt = conn.prepareStatement(deleteSql)) {
                deleteStmt.setString(1, departmentName);
                int affected = deleteStmt.executeUpdate();
                return affected > 0;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }

    // 查询所有部门信息
    public List<Department> getAllDepartments() {
        List<Department> list = new ArrayList<>();
        String sql = "SELECT * FROM departments";
        try (Connection conn = DBConnection.getConnection();
             Statement stmt = conn.createStatement();
             ResultSet rs = stmt.executeQuery(sql)) {
            while (rs.next()) {
                Department dept = new Department(
                        rs.getString("department_name"),
                        rs.getString("description")
                );
                list.add(dept);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }
}
